package com.xspace.android.imcommon.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

import com.xspace.android.imcommon.entity.DBSMSBean;

import java.util.ArrayList;
import java.util.List;

public class BaseSMSDBBiz extends BaseDb {
	public String smsId = "_ID";
	public String shortContent = "sContent";
	public String longContent = "lContent";
	public String uuid = "uuid";
	public String date = "date";
	public String msgState = "state";
	public String TABLE_NAME = "";

	public BaseSMSDBBiz() {
	}

	public BaseSMSDBBiz(Context context) {
		super(context);
	}

	public String getTable() {
		return "CREATE TABLE " + TABLE_NAME + " (" + smsId + " INTEGER PRIMARY KEY AUTOINCREMENT," + shortContent + " text," + longContent + " text," + date
				+ " text," + uuid + " text," + msgState + " INTEGER)";
	}

	@Override
	public String getTableName() {
		return TABLE_NAME;
	}

	public int saveSMS(DBSMSBean info) {
		ContentValues values = new ContentValues();
		values.put(this.shortContent, info.shortContent);
		values.put(this.longContent, info.longContent);
		values.put(this.uuid, info.uuid);
		values.put(this.date, info.date);
		values.put(this.msgState, info.state);
		Cursor cursor = null;
		try {
			long rowId = -1;
			checkDb();
			rowId = db.insert(TABLE_NAME, null, values);
			return (int) rowId;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		} finally {
			closeDbAndCursor(cursor);
		}
	}

	public ArrayList<DBSMSBean> querySMS(String uuids) {
		ArrayList<DBSMSBean> list = new ArrayList<DBSMSBean>();
		String sql = "select * from " + TABLE_NAME + " where " + uuid + " ='" + uuids + "'  order by " + date + " desc";
		System.out.println("=====================sql:" + sql);
		Cursor cursor = null;
		try {
			checkDb();
			cursor = db.rawQuery(sql, null);
			System.out.println("=====================cursor.getCount():" + cursor.getCount());
			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				for (int i = 0; i < cursor.getCount(); i++) {
					cursor.moveToPosition(i);
					DBSMSBean bean = new DBSMSBean();
					bean.smsId = cursor.getInt(0);
					bean.shortContent = cursor.getString(1);
					bean.longContent = cursor.getString(2);
					bean.uuid = cursor.getString(3);
					bean.date = cursor.getString(4);
					list.add(bean);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeDbAndCursor(cursor);
		}
		return list;
	}

	public int updateSMS(String uuids) {
		ContentValues values = new ContentValues();
		values.put(this.msgState, 0);
		try {
			long rowId = -1;
			checkDb();
			rowId = db.update(TABLE_NAME, values, uuid + "=? and " + msgState + ">?", new String[] { uuid, "" + 0 });
			return (int) rowId;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	public int queryUnReadSMS(String uuids) {
		int size = 0;
		List<DBSMSBean> list = new ArrayList<DBSMSBean>();
		String sql = "select * from " + TABLE_NAME + " where " + uuid + " ='" + uuids + "' and state >" + 0;
		System.out.println("=====================sql:" + sql);
		Cursor cursor = null;
		try {
			checkDb();
			cursor = db.rawQuery(sql, null);
			System.out.println("=====================cursor.getCount():" + cursor.getCount());
			if (cursor != null && cursor.getCount() > 0) {
				size = cursor.getCount();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeDbAndCursor(cursor);
		}
		return size;
	}

	public boolean deleteSMS(int id) {
		try {
			checkDb();
			String whereClause = String.format(" %s = ?", smsId);
			String[] whereArgs = new String[] { "" + id };
			int res = db.delete(TABLE_NAME, whereClause, whereArgs);
			return res > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

}
